package Factories;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import DAO.InterfaceDAO.AdministrateurDAO;
import DAO.InterfaceDAO.ClasseDAO;
import DAO.InterfaceDAO.CompteDAO;
import DAO.InterfaceDAO.CourDAO;
import DAO.InterfaceDAO.EtudiantDAO;
import DAO.InterfaceDAO.ExerciceDAO;
import DAO.InterfaceDAO.LibraryDAO;
import DAO.InterfaceDAO.ProfesseurDAO;
import DAO.InterfaceDAO.ResultatDAO;
import DAO.MysqlDAO.AdministrateurDAO_Mysql;
import DAO.MysqlDAO.ClasseDAO_Mysql;
import DAO.MysqlDAO.CompteDAO_Mysql;
import DAO.MysqlDAO.CourDAO_Mysql;
import DAO.MysqlDAO.EtudiantDAO_Mysql;
import DAO.MysqlDAO.ExerciceDAO_Mysql;
import DAO.MysqlDAO.LibraryDAO_Mysql;
import DAO.MysqlDAO.ProfesseurDAO_Mysql;
import DAO.MysqlDAO.ResultatDAO_Mysql;

public class FactoryMySqlDAO extends FactoryDAO {

	private static FactoryMySqlDAO instance;

	protected static Connection cx;
	/** nom de la base de donnees */
	private String base = "learnenglish";
	/** chemin vers le serveur */
	private static String serveur = "jdbc:mysql://localhost/";
	/** nom utilisateur */
	private String util="root";
	/** mot de passe */
	private String passwd="";
	/** driver */
	private String driver = "com.mysql.jdbc.Driver";

	private FactoryMySqlDAO() {
		super();
		try {
			Class.forName(driver).newInstance();
			String str;	
			str = serveur+base+"?user="+util+"&password="+passwd;
			cx = DriverManager.getConnection(str);
		} catch (ClassNotFoundException e) {
			System.out.println("Impossible de demarrer le driver : " + e.getMessage());
		} catch (SQLException e) {
			System.out.println("Impossible d'obtenir la connexion.");
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	public static FactoryDAO getInstance() {
		if(instance == null)
			instance = new FactoryMySqlDAO();
		return instance;
	}

	@Override
	public AdministrateurDAO createAdminDAO() {
		return (AdministrateurDAO) new AdministrateurDAO_Mysql(cx);
	}

	@Override
	public ProfesseurDAO createProfDAO() {
		return (ProfesseurDAO) new ProfesseurDAO_Mysql(cx);
	}

	@Override
	public EtudiantDAO createEtudDAO() {
		return (EtudiantDAO) new EtudiantDAO_Mysql(cx);
	}

	@Override
	public ClasseDAO createClasseDAO() {
		return (ClasseDAO) new ClasseDAO_Mysql(cx);
	}

	@Override
	public CompteDAO createCompteDAO() {
		return (CompteDAO) new CompteDAO_Mysql(cx);
	}

	@Override
	public ResultatDAO createResultatDAO() {
		return (ResultatDAO) new ResultatDAO_Mysql(cx);
	}

	@Override
	public LibraryDAO createLibDAO() {
		return (LibraryDAO) new LibraryDAO_Mysql(cx);
	}

	@Override
	public CourDAO createCourDAO() {
		return (CourDAO) new CourDAO_Mysql(cx);
	}

	@Override
	public ExerciceDAO createExerciceDAO() {
		return (ExerciceDAO) new ExerciceDAO_Mysql(cx);
	}

}
